package com.kotlin.android.simple.batch

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.kotlin.android.api.CallResult
import com.kotlin.android.api.RetrofitManager
import com.kotlin.android.api.api.*
import com.kotlin.android.api.base.BaseUIModel
import com.kotlin.android.api.bean.Batch
import com.kotlin.android.api.bean.PostInfo
import com.kotlin.android.api.config.ApiConfig
import com.kotlin.android.core.BaseViewModel
import com.kotlin.android.ktx.ext.Http
import com.kotlin.android.ktx.ext.e
import kotlinx.coroutines.launch

/**
 *
 * Created on 2020/5/14.
 *
 * @author o.s
 */
class BatchViewModel : BaseViewModel() {

    private val api by lazy { RetrofitManager.api(Api::class.java) }

    private val _uiState = MutableLiveData<BatchUIModel>()

    val uiState: LiveData<BatchUIModel> = _uiState

    fun loadData() {
        viewModelScope.launch(main) {
            CallResult<Batch>().error {
                emitUIState(error = it)
            }.success {
                val uiModel = BatchUIModel()
                it.result.forEach { result ->
                    "batch result :: $result".e()
                    when (result.path) {
                        ApiMisc.Path.SEAT_ICONS -> {
                            uiModel.seatIcons = result.data
                        }
                        ApiMisc.Path.SERVICE_CALL -> {
                            uiModel.serviceCall = result.data
                        }
                        ApiMisc.Path.URL_CONFIG -> {
                            uiModel.urlConfig = result.data
                        }
                        ApiMisc.Path.PROMPT_CONFIG -> {
                            uiModel.promptConfig = result.data
                        }
                        ApiMisc.Path.SKINS -> {
                            uiModel.skins = result.data
                        }
                        ApiMisc.Path.APP_FLASH_AD -> {
                            uiModel.appFlashed = result.data
                        }
                        ApiUser.Path.DEVICE_TOKEN -> {
                            uiModel.deviceToken = result.data
                        }
                        ApiCoupon.Path.COUPON_BIND_COUNT -> {
                            uiModel.couponBindCount = result.data
                        }
                        ApiCoupon.Path.COUPON_EXPIRE_SOON -> {
                            uiModel.couponExpireSoon = result.data
                        }
                        ApiCard.Path.USER_CARD_EXPIRE_SOON -> {
                            uiModel.userCardExpireSoon = result.data
                        }
                        else -> {
//                            uiModel.urlConfig = result.data
                        }
                    }
                }
                emitUIState(uiModel)
            }.call {
                val list = ArrayList<PostInfo>()
                list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.SEAT_ICONS))
                list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.SERVICE_CALL))
                list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.URL_CONFIG))
                list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.PROMPT_CONFIG))
                list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.SKINS))
                list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.APP_FLASH_AD))
//                list.add(PostInfo.create(Http.POST, ApiConfig.LOCAL_DNS_USER, ApiUser.Path.DEVICE_TOKEN, "deviceToken" to "储存极光推送RegistrationID参数"))
//                val postJson = PostInfo.toJson(
//                    PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.SEAT_ICONS),
//                    PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.SERVICE_CALL),
//                    PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.URL_CONFIG),
//                    PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.PROMPT_CONFIG),
//                    PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.SKINS),
//                    PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_MISC, ApiMisc.Path.APP_FLASHED),
//                    PostInfo.create(
//                        Http.POST,
//                        ApiConfig.LOCAL_DNS_USER,
//                        ApiUser.Path.DEVICE_TOKEN,
//                        "deviceToken" to "储存极光推送RegistrationID参数"
//                    )
//                )
//                if (isLogin) {
//                    list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_COUPON, ApiCoupon.Path.COUPON_BIND_COUNT, "member_id" to "uid"))
//                    if (isToday(expireSoonTime)) {
//                        list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_COUPON, ApiCoupon.Path.COUPON_EXPIRE_SOON, "member_id" to "uid"))
//                        list.add(PostInfo.create(Http.GET, ApiConfig.LOCAL_DNS_CARD, ApiCard.Path.USER_CARD_EXPIRE_SOON))
//                    }
//                }
                val postJson = PostInfo.toJson(list)
                postJson.e()
                api.getBatch(postJson)
            }
        }
    }

    private fun emitUIState(uiModel: BatchUIModel) {
        _uiState.value = uiModel
    }

    private fun emitUIState(
        seatIcons: String? = null,
        serviceCall: String? = null,
        urlConfig: String? = null,
        promptConfig: String? = null,
        skins: String? = null,
        appFlashed: String? = null,
        deviceToken: String? = null,
        couponBindCount: String? = null,
        couponExpireSoon: String? = null,
        userCardExpireSoon: String? = null,
        showLoading: Boolean = false,
        isRefresh: Boolean = false,
        loadMore: Boolean = false,
        error: String? = null,
        netError: String? = null,
        isEmpty: Boolean = false,
        success: String? = null
    ) {
        val model = BatchUIModel()
        model.showLoading = showLoading
        model.isRefresh = isRefresh
        model.loadMore = loadMore
        model.error = error
        model.netError = netError
        model.isEmpty = isEmpty
        model.success = success
        model.seatIcons = seatIcons
        model.serviceCall = serviceCall
        model.urlConfig = urlConfig
        model.promptConfig = promptConfig
        model.skins = skins
        model.appFlashed = appFlashed
        model.deviceToken = deviceToken
        model.couponBindCount = couponBindCount
        model.couponExpireSoon = couponExpireSoon
        model.userCardExpireSoon = userCardExpireSoon

        _uiState.value = model
    }

    data class BatchUIModel(
        var seatIcons: String? = null,
        var serviceCall: String? = null,
        var urlConfig: String? = null,
        var promptConfig: String? = null,
        var skins: String? = null,
        var appFlashed: String? = null,
        var deviceToken: String? = null,
        var couponBindCount: String? = null,
        var couponExpireSoon: String? = null,
        var userCardExpireSoon: String? = null
    ) : BaseUIModel<String>()
}